﻿using System.Text;
using System.Windows.Forms;


namespace Pixysoft.RTF
{
    public partial class RtfBuilder
    {
        #region Nested type: RTFCellDefinitionBuilder

        private class RTFCellDefinitionBuilder
        {
            #region Fields

            private readonly RtfBuilder _builder;
            private readonly StringBuilder _definitionBuilder;
            private RTFCellDefinition _cellDefinition;

            #endregion

            #region Constructor

            internal RTFCellDefinitionBuilder(RtfBuilder builder, StringBuilder definitionBuilder, RTFCellDefinition cellDefinition)
            {
                this._builder = builder;

                this._definitionBuilder = definitionBuilder;
                this._cellDefinition = cellDefinition;


                this.AppendDefinition();
            }

            #endregion

            #region Public Properties

            public RTFCellDefinition CellDefinition
            {
                get { return this._cellDefinition; }
            }

            #endregion

            #region Methods

            private void AppendDefinition()
            {
                this.CellAlignment();
                this.TableCellBorderSide();
                //Pad();


                this._definitionBuilder.AppendFormat("\\cellx{0}", (int)(this._cellDefinition.CellWidthRaw * TWIPSA4) + this._cellDefinition.X);
                //_definitionBuilder.AppendFormat("\\clwWidth{0}", _cellDefinition.CellWidth);

                //_definitionBuilder.Append("\\cltxlrtb\\clFitText");

                this._definitionBuilder.AppendLine();


                //Cell text flow
            }

            private string BorderDef()
            {
                StringBuilder sb = new StringBuilder();
                RtfBorderSide _rTFBorderSide = this._cellDefinition.RTFBorderSide;
                if ((_rTFBorderSide & RtfBorderSide.DoubleThickness) == RtfBorderSide.DoubleThickness)
                {
                    sb.Append("\\brdrth");
                }
                else
                {
                    sb.Append("\\brdrs");
                }
                if ((_rTFBorderSide & RtfBorderSide.DoubleBorder) == RtfBorderSide.DoubleBorder)
                {
                    sb.Append("\\brdrdb");
                }
                sb.Append("\\brdrw");
                sb.Append(this._cellDefinition.BorderWidth);

                sb.Append("\\brdrcf");
                sb.Append(this._builder.ColorTable.IndexOf(this._cellDefinition.BorderColor));

                return sb.ToString();
            }

            private void CellAlignment()
            {
                switch (this._cellDefinition.Alignment)
                {
                    case RtfAlignment.BottomCenter:
                    case RtfAlignment.BottomLeft:
                    case RtfAlignment.BottomRight:
                        this._definitionBuilder.Append("\\clvertalb"); //\\qr
                        break;
                    case RtfAlignment.MiddleCenter:
                    case RtfAlignment.MiddleLeft:
                    case RtfAlignment.MiddleRight:
                        this._definitionBuilder.Append("\\clvertalc"); //\\qr
                        break;
                    case RtfAlignment.TopCenter:
                    case RtfAlignment.TopLeft:
                    case RtfAlignment.TopRight:
                        this._definitionBuilder.Append("\\clvertalt"); //\\qr
                        break;
                }
            }

            private void Pad()
            {
                if (this._cellDefinition.Padding != Padding.Empty)
                {
                    StringBuilder sb = this._definitionBuilder;
                    sb.AppendFormat("\\clpadfl3\\clpadl{0}", this._cellDefinition.Padding.Left);
                    sb.AppendFormat("\\clpadlr3\\clpadr{0}", this._cellDefinition.Padding.Right);
                    sb.AppendFormat("\\clpadlt3\\clpadt{0}", this._cellDefinition.Padding.Top);
                    sb.AppendFormat("\\clpadlb3\\clpadb{0}", this._cellDefinition.Padding.Bottom);
                }
            }

            private void TableCellBorderSide()
            {
                RtfBorderSide _rTFBorderSide = this._cellDefinition.RTFBorderSide;

                if (_rTFBorderSide != RtfBorderSide.None)
                {
                    StringBuilder sb = this._definitionBuilder;
                    string bd = this.BorderDef();
                    if (_rTFBorderSide == RtfBorderSide.None)
                    {
                        sb.Append("\\brdrnil");
                    }
                    else
                    {
                        if ((_rTFBorderSide & RtfBorderSide.Left) == RtfBorderSide.Left)
                        {
                            sb.Append("\\clbrdrl").Append(bd);
                        }
                        if ((_rTFBorderSide & RtfBorderSide.Right) == RtfBorderSide.Right)
                        {
                            sb.Append("\\clbrdrr").Append(bd);
                        }
                        if ((_rTFBorderSide & RtfBorderSide.Top) == RtfBorderSide.Top)
                        {
                            sb.Append("\\clbrdrt").Append(bd);
                        }
                        if ((_rTFBorderSide & RtfBorderSide.Bottom) == RtfBorderSide.Bottom)
                        {
                            sb.Append("\\clbrdrb").Append(bd);
                        }
                    }
                }
            }

            #endregion
        }

        #endregion
    }
}


